package com.bishe.psy.controller;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.model.SCL90;
import com.bishe.psy.model.Sleep;
import com.bishe.psy.model.StuUser;
import com.bishe.psy.service.ISleepService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javafx.beans.binding.SetBinding;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-26
 * Time: 18:12
 */

@Slf4j
@Api(tags = "sleep测试问卷接口")
@RestController
@RequestMapping("/sleep")
public class SleepController {

    @Resource
    private ISleepService iSleepService;


    @ApiOperation("新增sleep测试数据接口")
    @GetMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "sleep选项分值汇总") @RequestParam("data[]") String[]  data) throws ParseException {


        //1.参数校验
        if(data.length <= 0) {
            return AppResult.failed("测试出错了");
        }

        //2.获取当前登录的用户信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        int asleepNum = 0;
        int sleepDisturbancesNum = 0;
        int dysfunctionNum = 0;
        DateFormat df = new SimpleDateFormat("HH:mm");
        int goBedHours = 0;
        int goBedMinutes = 0;
        int setUpHours = 0;
        int setUpMinutes = 0;
        int sleepTimeNum = 0;
        int sleepEfficiencyNum = 0;

        int quality = 0;                  //睡眠质量
        int asleep = 0;                   //入睡时间
        int sleepTime = 0;                //睡眠时间
        int sleepEfficiency = 0;          //睡眠效率
        int sleepDisturbances = 0;        //睡眠障碍
        int hypnoticDrugs = 0;            //催眠药物
        int dysfunction = 0;              //日间功能障碍
        int score = 0;                    //PISQ总分


        //3.获取data里面的数据
        for(int i = 1; i < data.length + 1; i++ ) {

            //计算入睡时间，选择提2和5的选项分值和
            if(i == 1 || i == 2) {
                asleepNum = asleepNum + Integer.parseInt(data[i]);
            }

            //计算睡眠障碍，选择题6~14
            if(i == 3 || i == 4 || i == 5 || i == 6 || i == 7 || i == 8 || i == 9 || i == 10 || i == 11 ){
                sleepDisturbancesNum = sleepDisturbancesNum + Integer.parseInt(data[i]);
            }


            //计算睡眠质量最终值,选择题15
            if(i == 12) {
                quality = quality + Integer.parseInt(data[i]);
            }

            //计算催眠药物最终值，选择题16
            if(i == 13){
                hypnoticDrugs = hypnoticDrugs + Integer.parseInt(data[i]);
            }

            //计算日间功能障碍，选择题17~18
            if(i == 14 || i == 15 ) {
                dysfunctionNum = dysfunctionNum + Integer.parseInt(data[i]);
            }

            //计算睡眠效率
            if(i == 16) {
                System.out.println("检查上床时间");
                goBedHours = Integer.parseInt(data[i].substring(0,2));
                goBedMinutes = Integer.parseInt(data[i].substring(3,5));
                System.out.println("上床小时 = " + goBedHours);
                System.out.println("上床分钟 = " +  goBedMinutes);

            }
            if(i == 17) {
                System.out.println("检查起床时间");
                setUpHours = Integer.parseInt(data[i].substring(0,2));
                setUpMinutes = Integer.parseInt(data[i].substring(3,5));
                System.out.println("起床小时 = " + setUpHours);
                System.out.println("起床分钟 = " +  setUpMinutes);
            }


            //计算睡眠时间最终值
            if(i == 18) {
                sleepTimeNum = Integer.parseInt(data[i]);
                if( sleepTimeNum  < 5 ) {
                    sleepTime = 3;
                } else if( sleepTimeNum  >= 5 && sleepTimeNum  < 6) {
                    sleepTime = 2;
                } else if ( sleepTimeNum  >= 6 &&  sleepTimeNum  < 7) {
                    sleepTime = 1;
                } else {
                    sleepTime = 0;
                }
            }

        }


        //计算入睡时间最终值
        if(asleepNum == 0) {
            asleep = 0;
        } else if(asleepNum >= 1 && asleepNum < 3) {
            asleep = 1;
        } else if(asleepNum >= 3 && asleepNum < 5){
            asleep = 2;
        } else if(asleepNum >= 5 && asleepNum < 7){
            asleep = 3;
        }

        //计算睡眠障碍最终值
        if(sleepDisturbancesNum == 0) {
            sleepDisturbances = 0;
        } else if(sleepDisturbancesNum >=1 && sleepDisturbancesNum < 10) {
            sleepDisturbances = 1;
        } else if(sleepDisturbancesNum >=10 && sleepDisturbancesNum < 19) {
            sleepDisturbances = 2;
        } else if(sleepDisturbancesNum >=19 && sleepDisturbancesNum < 28) {
            sleepDisturbances = 3;
        }

        //计算日间功能障碍最终值
        if(dysfunctionNum == 0) {
            dysfunction = 0;
        } else if(dysfunctionNum >= 1 && dysfunctionNum < 3) {
            dysfunction = 1;
        } else if(dysfunctionNum >= 3 && dysfunctionNum < 5) {
            dysfunction = 2;
        }else if(dysfunctionNum >= 5 && dysfunctionNum < 7) {
            dysfunction = 3;
        }

        //计算最终睡眠效率
        int bedTimeHours = 24 - goBedHours + setUpHours;
        double bedTime = 0;
        if((goBedMinutes == 30 && setUpMinutes == 30) || (goBedMinutes == 0 && setUpMinutes == 0) ) {
             bedTime = bedTimeHours;
        } else if(goBedMinutes == 0 && setUpMinutes == 30) {
            bedTime = bedTimeHours + 0.5;
        } else {
            bedTime = bedTimeHours - 0.5;
        }
        sleepEfficiencyNum = (int) (( sleepTimeNum  / bedTime) * 100);
        if(sleepEfficiencyNum < 65) {
            sleepEfficiency = 3;
        } else if(sleepEfficiencyNum >= 65 && sleepEfficiencyNum < 75) {
            sleepEfficiency = 2;
        } else if(sleepEfficiencyNum >= 75 && sleepEfficiencyNum < 85) {
            sleepEfficiency = 1;
        }else {
            sleepEfficiency = 0;
        }


        //计算总分
        score = quality + asleep + sleepTime + sleepEfficiency + sleepDisturbances + hypnoticDrugs + dysfunction;


        //4.创建对象
        Sleep sleep = new Sleep();
        sleep.setUserId(stuUser.getId());
        sleep.setQuality(quality);
        sleep.setAsleep(asleep);
        sleep.setSleepTime(sleepTime);
        sleep.setSleepEfficiency(sleepEfficiency);
        sleep.setSleepDisturbances(sleepDisturbances);
        sleep.setHypnoticDrugs(hypnoticDrugs);
        sleep.setDysfunction(dysfunction);
        sleep.setScore(score);

        System.out.println("睡眠质量 = " + quality);
        System.out.println("入睡时间 = " + asleep);
        System.out.println("睡眠时间 = " +sleepTime);
        System.out.println("睡眠效率 = " + sleepEfficiency);
        System.out.println("睡眠障碍 = " + sleepDisturbances);
        System.out.println("催眠药物 = " + hypnoticDrugs);
        System.out.println("日间功能障碍 = " + dysfunction);
        System.out.println("总分 = " + score);
        System.out.println("床上时间 = " + bedTime);


        //5.调用service
        iSleepService.create(sleep);

        //6.返回结果
        return AppResult.success();

    }


    @ApiOperation("查询最新sleep测试问卷记录接口")
    @GetMapping("/select")
    public AppResult<Sleep> selectById(HttpServletRequest request) {

        //1.获取登录信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        Sleep sleep = iSleepService.selectByIdNewResult(stuUser.getId());

        //3.校验参数
        if(sleep == null || sleep.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //4.返回结果
        return AppResult.success(sleep);

    }


    //根据用户id查询sleep测评记录
    @ApiOperation("查询用户的所有测评记录")
    @GetMapping("/selectAll")
    public AppResult<List<Sleep>> selectByUserId(HttpServletRequest request) {

        //1.获取用户信息
        HttpSession session = request.getSession(false);
        StuUser stuUser = (StuUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.调用service
        List<Sleep> sleepList = iSleepService.selectByUserId(stuUser.getId());

        //3.返回数据
        return AppResult.success(sleepList);

    }

    //根据用户id查询sleep测评记录
    @ApiOperation("查询用户的所有测评记录")
    @GetMapping("/selectAllCon")
    public AppResult<List<Sleep>> selectByUserIdCon(Long id) {

        //1.参数校验
        if(id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED);
        }
        //2.调用service
        List<Sleep> sleepList = iSleepService.selectByUserId(id);

        //3.返回数据
        return AppResult.success(sleepList);

    }
}
